package com.cdm.persistence;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * CdmContent entity.
 * 
 * @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "CDM_CONTENT", catalog = "CDM", uniqueConstraints = {})
public class Content implements java.io.Serializable {

	// Fields

	private Long dcid;
	private String contentName;
	private ContentType contentType;
	private ContentState contentState;

	private Long contentLength;

	private String description;

	private User cp;

	private String actor;
	private String director;
	private String productor;
	private Date limitTime;

	public static void main(String[] args) {
		System.out.println(EntityManagerHelper.getEntityManager().getTransaction().isActive());
		EntityManagerHelper.beginTransaction();
		System.out.println(EntityManagerHelper.getEntityManager().getTransaction().isActive());
		EntityManagerHelper.getEntityManager().getTransaction().setRollbackOnly();
		System.out.println(EntityManagerHelper.getEntityManager().getTransaction().isActive());
		EntityManagerHelper.commit();
		System.out.println(EntityManagerHelper.getEntityManager().getTransaction().isActive());
		//		EntityManagerHelper.beginTransaction();

		// Content c = new Content();
		// c.setDcid(1001020l);
		// c.setActor("abcd");
		// c.setContentName("content name");
		// c.setCp(new UserDAO().get(1l));
		// c.setContentState(new ContentState(10010201l, 1));
		//		

		// Content c = EntityManagerHelper.getEntityManager().find(Content.class, 1001020l);
		// System.out.println(c == null ? "null" : c.getContentState().getContentState());
	}

	// Constructors

	/** default constructor */
	public Content() {
	}

	public String createXML() {
		Document document = null;
		try {
			SAXReader reader = new SAXReader();
			document = reader.read(Content.class.getResourceAsStream("EncoderInfoHead.xml"));

			Element root = document.getRootElement();
			Element root2 = reader.read(Content.class.getResourceAsStream("EncoderInfo2.xml")).getRootElement();
			root2.addElement("DOI").addText("TBD");

			Element iend = root2.addElement("Identifiers").addElement("Identifier");
			iend.addElement("IdentifierType").addText("ISWC");
			iend.addElement("IdentifierValue").addText("T-034.524.680");

			Element Title = root2.addElement("Titles").addElement("Title");
			Title.addElement("TitleValue").addText(this.getContentName());
			Title.addElement("Language").addText("en");

			root2.addElement("StructuralType").addText(this.getContentType().getName());

			root2.addElement("Modes").addElement("Mode").addText(this.getContentType().getName());

			Element PrimaryAgents = root2.addElement("PrimaryAgents");
			Element Agent = PrimaryAgents.addElement("Agent").addElement("sequence", "1");
			Element name = Agent.addElement("Name");
			name.addElement("NameType").addText("Person");
			name.addElement("NameValue").addText(this.getActor());
			name.addElement("Roles").addElement("Role").addText("Artist");

			Element Agent2 = PrimaryAgents.addElement("Agent").addElement("sequence", "2");
			Element name2 = Agent2.addElement("Name");
			name2.addElement("NameType").addText("Person");
			name2.addElement("NameValue").addText(this.getDirector());
			name2.addElement("Roles").addElement("Role").addText("Director");

			Element Agent3 = PrimaryAgents.addElement("Agent").addElement("sequence", "3");
			Element name3 = Agent3.addElement("Name");
			name3.addElement("NameType").addText(this.getProductor());
			name3.addElement("NameValue").addText(this.getDirector());
			name3.addElement("Roles").addElement("Role").addText("Productor");

			Element Assertor = root2.addElement("Assertor");
			Element Registrant = Assertor.addElement("Registrant");
			Element rname = Registrant.addElement("Name");
			rname.addElement("NameType").addText("Person");
			rname.addElement("NameValue").addText("Persons Name Here");

			Element Identifier = Registrant.addElement("Identifier");
			Identifier.addElement("IdentifierType").addText("PublisherCode");
			Identifier.addElement("IdentifierValue").addText("9901");

			Element Authority = Assertor.addElement("Authority");
			Element aname = Authority.addElement("Name");
			aname.addElement("NameType").addText("Organization");
			aname.addElement("NameValue").addText("Objectlab");

			root.add(root2);

		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return document.asXML();

	}

	private Element addNameValuePair(Element root, String name, String value) {
		root.addElement("IdentifierType").addText(name);
		root.addElement("IdentifierValue").addText(value);
		return root;
	}

	// Property accessors
	@Id
	@Column(name = "dcid", unique = true, nullable = false, insertable = true, updatable = true)
	public Long getDcid() {
		return this.dcid;
	}

	public void setDcid(Long dcid) {
		this.dcid = dcid;
	}

	@Column(name = "content_name", unique = false, nullable = false, insertable = true, updatable = true, length = 1000)
	public String getContentName() {
		return this.contentName;
	}

	public void setContentName(String contentName) {
		this.contentName = contentName;
	}

	@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)
	@JoinColumn(name = "content_type", referencedColumnName = "id")
	public ContentType getContentType() {
		return contentType;
	}

	public void setContentType(ContentType contentType) {
		this.contentType = contentType;
	}

	@OneToOne(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
	@JoinColumn(name = "content_state", referencedColumnName = "dcid")
	public ContentState getContentState() {
		return contentState;
	}

	public void setContentState(ContentState contentState) {
		this.contentState = contentState;
	}

	@Column(name = "content_length")
	public Long getContentLength() {
		return this.contentLength;
	}

	public void setContentLength(Long contentLength) {
		this.contentLength = contentLength;
	}

	@Column(name = "description", length = 65535)
	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@ManyToOne(cascade = CascadeType.REFRESH)
	@JoinColumn(name = "cpid", referencedColumnName = "user_id")
	public User getCp() {
		return cp;
	}

	public void setCp(User cp) {
		this.cp = cp;
	}

	@Column(name = "actor", length = 500)
	public String getActor() {
		return this.actor;
	}

	public void setActor(String actor) {
		this.actor = actor;
	}

	@Column(name = "director", length = 100)
	public String getDirector() {
		return this.director;
	}

	public void setDirector(String director) {
		this.director = director;
	}

	@Column(name = "productor", length = 100)
	public String getProductor() {
		return this.productor;
	}

	public void setProductor(String productor) {
		this.productor = productor;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "limit_time", length = 19)
	public Date getLimitTime() {
		return this.limitTime;
	}

	public void setLimitTime(Date limitTime) {
		this.limitTime = limitTime;
	}

}